#include<windows.h>


BOOL StopDriver(SC_HANDLE scm , LPCTSTR DriverName)
{
	SC_HANDLE Service = OpenService(scm , DriverName , SERVICE_ALL_ACCESS);
	if(Service == NULL)
	{
		MessageBox(NULL,"Error Can't stop driver!","Error:", MB_ICONERROR);
		return FALSE;
	}
	SERVICE_STATUS  serviceStatus;
	BOOL ret = ControlService(Service , SERVICE_CONTROL_STOP , &serviceStatus);
	if(!ret)
	{
		DWORD err = GetLastError();
	}
	CloseServiceHandle(Service);
	return ret;
}

BOOL StartDriver(SC_HANDLE scm , LPCTSTR DriverName)
{
	SC_HANDLE Service = OpenService(scm , DriverName , SERVICE_ALL_ACCESS);
	if( Service == NULL )
	{
		MessageBox(NULL,"Can't Open Service!","Error", MB_ICONERROR);
	}
    BOOL ret = StartService(Service , 0 , NULL);
	if(!ret)
	{
		DWORD err = GetLastError( );
		if( err == ERROR_SERVICE_ALREADY_RUNNING )
		{
			MessageBox(NULL,"Service all ready running!","Information", MB_ICONERROR);
			ret = TRUE;
		}
		else
		{
			MessageBox(NULL,"Can't Start Driver","Error!", MB_ICONERROR);
		}
	}
		CloseServiceHandle(Service);
		return ret;
}

BOOL InstallDriver(SC_HANDLE scm , LPCTSTR DriverName , LPCTSTR DriverPath)
{
	SC_HANDLE Service = CreateService(scm , 
		                              DriverName ,
									  DriverName ,
									  SERVICE_ALL_ACCESS,
									  SERVICE_KERNEL_DRIVER ,
									  SERVICE_DEMAND_START ,
									  SERVICE_ERROR_IGNORE ,
									  DriverPath,
									  NULL,
									  NULL,
									  NULL,
									  NULL,
									  NULL);
	if(Service == NULL)
	{
		DWORD err = GetLastError( );
		if( err == ERROR_SERVICE_EXISTS ) /* service allready started*/
		{
	//		MessageBox(NULL,"Service Allready Started!","Information",  MB_ICONINFORMATION);
		}
		else
		{
			MessageBox(NULL,"Can't Start Service!","Error!",  MB_ICONERROR);
		
		}
		return FALSE;
	}
	    CloseServiceHandle(Service);
		return TRUE;
}